<?php
class Exam_ExamController extends Core_Controller_Action_Standard {
  public function init()
  {
    if( !$this->_helper->requireAuth()->setAuthParams('exam', null, 'view')->isValid() ) return;

    else if( 0 !== ($exam_id = (int) $this->_getParam('exam_id')) &&
        null !== ($exam = Engine_Api::_()->getItem('exam', $exam_id)) )
    {
      Engine_Api::_()->core()->setSubject($exam);
    }
  }
  public function qtmanageAction(){
  	if( !$this->_helper->requireUser()->isValid() ) return;
    if( !$this->_helper->requireSubject('exam')->isValid() ) return;
    if( !$this->_helper->requireAuth()->setAuthParams(null, null, 'edit')->isValid() ) return;

    // Get navigation
    $this->view->navigation = $navigation = Engine_Api::_()->getApi('menus', 'core')
      ->getNavigation('exam_main', array(), 'exam_main_exam');

    // Prepare data
    $this->view->exam = $exam = Engine_Api::_()->core()->getSubject();
  }
  
  public function addNewQuestionAction(){
  	//check valid exam
  	if( !$this->_helper->requireUser()->isValid() ) return;
  	if( !$this->_helper->requireAuth()->setAuthParams('exam', null, 'create')->isValid()) return;
  	$viewer = Engine_Api::_()->user()->getViewer();
        $this->view->tinyMce()->setOptions(Engine_Api::_()->exam()->initTinyMCE());
  	$this->view->form = $form = new Exam_Form_Exam_Addquestion();
	$this->view->isSuccess = false;
	$this->view->exam = $exam = Engine_Api::_()->core()->getSubject();
        
  	// Not post/invalid
        if( !$this->getRequest()->isPost() ) {
          return;
        }
	    	    
	if( !$form->isValid($this->getRequest()->getPost()) ) {
		return;
	}

	// Process
	$values = $this->getRequest()->getPost();
	var_dump($values['answer']);die;
	$values['category'] = 1;
        $db = Engine_Api::_()->getItemTable('exam_question')->getAdapter();
  	$table_question = Engine_Api::_()->getItemTable('exam_question');
  	
  	$db->beginTransaction();
  	try {		 		
  		$question= $table_question->createRow();
  		$question->content = $values['content'];
  		$question->category_id = $values['category'];
  		$question->creation_date = date('Y-m-d H:i:s');
  		$question->modified_date   = date('Y-m-d H:i:s');
  		$question->owner_id = $viewer->getIdentity();
  		$question->save();  		
  		$db->commit();
  	
  	}catch (Exception $e)
  	{
  		$db->rollBack();
//  		throw $e;
  	}
  	
  	//add to Examdetail table
  	$table_detail = Engine_Api::_()->getDbTable('examdetails','exam');
  	
  	$detail = $table_detail->createRow();
  	$detail->exam_id = $exam->getIdentity();
  	$detail->question_id = $question->getIdentity();
  	$detail->order = $table_detail->getLastedOrder($exam->getIdentity()) + 1;
  	$detail->save();
  	
  	//Add answer to Answer table
  	$table_answer = Engine_Api::_()->getItemTable('exam_answer');
  	$number_choice = 0;
	$answer_order = 0;
  	foreach( $values['answer'] as $key=>$value)
  	{
  			//create new info
  			$answer = $table_answer->createRow();
  			$answer->question_id = $question->getIdentity();
  			$answer->content = $value;
  			$answer->explanation = '';
			$answer->order = ++$answer_order;
  			if(isset($values['answer_correct_'. $key]) && !empty($values['answer_correct_'. $key])){
  				$answer->result = 1;
  				$number_choice++;
  			}else{ 
  				$answer->result = 0;
  			}
  			$answer->save();
  	};
  	
  	//update $number_choice
  	$question->number_choice = $number_choice;        
  	$question->save();

        //edit exam
        $exam->total_question++;
        $exam->save();
	
	$this->view->isSuccess = true;
  }
  public function loadQuestionsAction(){
	 $this->view->exam = $exam = Engine_Api::_()->core()->getSubject();
  }
}
